package com.crm.base;

import com.crm.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * 基础控制器
 */
public class BaseController<M extends BaseService<T>, T> {

    @Autowired
    protected M baseService;

    @RequestMapping("/save")
    public Result<Boolean> save(T t) {
        return baseService.add(t) ? Result.success() : Result.failure();
    }

    @RequestMapping("/delete")
    public Result<Boolean> delete(Integer id) {
        return baseService.del(id) ? Result.success() : Result.failure();
    }

    @RequestMapping("/batchDelete")
    public Result<Boolean> batchDelete(String str) {
        return baseService.batchDel(str.split(",")) ? Result.success() : Result.failure();
    }

    @RequestMapping("/update")
    public Result<Boolean> update(T t) {
        return baseService.upd(t) ? Result.success() : Result.failure();
    }

    @RequestMapping("/get/{id}")
    public Result<T> findById(@PathVariable Integer id) {
        return Result.success(baseService.findById(id));
    }

    @RequestMapping("/list")
    public Result<List<T>> list(T t) {
        return Result.success(baseService.findAll(t));
    }
}
